var entityCollection = require('../../../Base/server/controllers/EntityCollection'),
    BaseEnum = require('../../../Base/server/controllers/Enum');

//获取系统参数值
exports.getParameterValue = function(classification, code, completeFun){
    var entity = entityCollection.getEntity("Parameter");
    entity.findOne({Classification: classification, Code: code}, null, function(err, record){
        if(err){
            var newErr = new Error();
            newErr.leval = 9;
            newErr.title = '系统查询错误';
            newErr.message = '通过编码查询系统参数失败，请检查查询条件';
            completeFun(newErr);
        }
        else {
            if(record != null){
                var value;
                if(record.Type == BaseEnum.ControllType.TextBox.Value){
                    value = record.Value;
                }
                else if(record.Type == BaseEnum.ControllType.CheckBox.Value){
                    if(record.Value == 'true'){
                        value = true;
                    }
                    else{
                        value = false;
                    }
                }
                else if(record.Type == BaseEnum.ControllType.NumberBox.Value){
                    value = parseFloat(record.Value);
                }
                else if(record.Type == BaseEnum.ControllType.PercentBox.Value){
                    value = parseFloat(record.Value);
                }
                else if(record.Type == BaseEnum.ControllType.DropDownList.Value){
                    value = parseInt(record.Value);
                }
                else if(record.Type == BaseEnum.ControllType.Refer.Value){
                    value = {
                        Key: record.Key,
                        Value: record.Value,
                        Text: record.Text
                    };
                }
                else if(record.Type == BaseEnum.ControllType.Calendar.Value){
                    if(record.CalendarType == BaseEnum.DateTimeFormat.Date.Value){
                        var date = new Date(record.Value);
                        value = new Date(date.getFullYear(), date.getMonth(), date.getDate());
                    }
                    else{
                        value = new Date(record.Value);
                    }
                }
                completeFun(null, value);
            }
            else{
                completeFun(null, null);
            }
        }
    });
};